//
// Created by Fronch Darly on 2021/3/3.
//
#include <jni.h>
#include <string>

extern "C" {
#include "lame.h"
}

#include "log-utils.h"
static lame_global_flags *lame = nullptr;



extern "C"{
    JNIEXPORT void JNICALL
    Java_com_darly_dlndk_module_lame_AudioRecorder_init(JNIEnv * env, jclass clazz, jint in_samplerate, jint in_channel, jint out_samplerate, jint out_bitrate, jint quality) {
        // TODO: implement init()
        showLog("开始进行初始化");
        LOGD("in_samplerate = %d,in_channel = %d,out_samplerate = %d,out_bitrate = %d,quality = %d,",in_samplerate,in_channel,out_samplerate,out_bitrate,quality);
        if (lame != nullptr) {
            lame_close(lame);
            lame = nullptr;
        }
        lame = lame_init();
        LOGD("init:",lame);
        lame_set_in_samplerate(lame, in_samplerate);
        lame_set_num_channels(lame, in_channel);
        lame_set_out_samplerate(lame, out_samplerate);
        lame_set_brate(lame, out_bitrate);
        lame_set_quality(lame, quality);
        lame_init_params(lame);
    }

    JNIEXPORT jint JNICALL
    Java_com_darly_dlndk_module_lame_AudioRecorder_encode(JNIEnv * env, jclass clazz, jshortArray buffer_left, jshortArray buffer_right, jint samples, jbyteArray mp3buf) {
        // TODO: implement encode()
        jshort *j_buffer_l = env->GetShortArrayElements(buffer_left, nullptr);

        jshort *j_buffer_r = env->GetShortArrayElements(buffer_right, nullptr);

        const jsize mp3buf_size = env->GetArrayLength(mp3buf);
        jbyte *j_mp3buf = env->GetByteArrayElements(mp3buf, nullptr);

        int result = lame_encode_buffer(lame, j_buffer_l, j_buffer_r,
                                        samples, reinterpret_cast<unsigned char *>(j_mp3buf),
                                        mp3buf_size);

        env->ReleaseShortArrayElements(buffer_left, j_buffer_l, 0);
        env->ReleaseShortArrayElements(buffer_right, j_buffer_r, 0);
        env->ReleaseByteArrayElements(mp3buf, j_mp3buf, 0);

        return result;
    }

    JNIEXPORT jint JNICALL
    Java_com_darly_dlndk_module_lame_AudioRecorder_flush(JNIEnv * env, jclass clazz, jbyteArray mp3buf ) {
        // TODO: implement flush()
        const jsize mp3buf_size = env->GetArrayLength(mp3buf);
        jbyte *j_mp3buf = env->GetByteArrayElements(mp3buf, nullptr);

        int result = lame_encode_flush(lame, reinterpret_cast<unsigned char *>(j_mp3buf), mp3buf_size);

        env->ReleaseByteArrayElements(mp3buf, j_mp3buf, 0);

        return result;
    }

    JNIEXPORT void JNICALL
    Java_com_darly_dlndk_module_lame_AudioRecorder_close(JNIEnv * env, jclass clazz ) {
        // TODO: implement close()
        lame_close(lame);
        lame = nullptr;
    }
};

